package view.component.window
{
import flash.errors.IllegalOperationError;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.IRenderProxy;
import framework.view.components.container.RepeaterProxy;

import utils.VectorUtil;

/**
 * PaginationWindowProxy
 * @author jliang
 * 
 */
public class PaginationWindowProxy extends WindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function PaginationWindowProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	public function get prevBtn():ButtonProxy
	{
		return proxys["prevBtn"];
	}

	public function get nextBtn():ButtonProxy
	{
		return proxys["nextBtn"];
	}
	
	public function get itemRepeater():RepeaterProxy
	{
		return proxys["list"];
	}
	
	protected var currentPage:int = 0;
	
	protected function get preCount():int
	{
		return itemRepeater.items.length;
	}
	
	protected function get repeaterItemRenderClass():Class
	{
		throw new IllegalOperationError("repeaterItemRenderClass getter should be overrided.");
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"prevBtn", classRef: ButtonProxy, id:"prevBtn", onClick: clickToGoPrevHandler },
			{ skin:"nextBtn", classRef: ButtonProxy, id:"nextBtn", onClick: clickToGoNextHandler },
			{ skin:"list", classRef: RepeaterProxy, id:"list", renderProxyRef:repeaterItemRenderClass, onItemSelected:listItemSelectedHandler },
		].concat(super.proxyMapping);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	/**
	 * return { data: Object, isEnd:Boolean } 
	 * @param source
	 * @param count
	 * @param index
	 * @return 
	 * 
	 */	
	protected function getPageData(source:*, count:int, index:int = 0):*
	{		
		var pageCount:int = Math.ceil(count / preCount);
		if (source is Array || VectorUtil.isVector(source))
		{			
			var isEnd:Boolean = index == pageCount - 1;
			if (count <= 0) isEnd = true;
			return { data: source.slice(index * preCount, index * preCount + preCount), isEnd: isEnd};
		}
		else
		{
			throw new IllegalOperationError("Can not identify the class type of source.");
		}
	}
	
	protected function updateList(source:*, count:int, index:int = 0):void
	{
		var obj:* = getPageData(source, count, index);
		
		itemRepeater.data = obj.data;
		currentPage = index;
		
		prevBtn.enabled = currentPage > 0;
		nextBtn.enabled = !obj.isEnd;
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	protected function clickToGoPrevHandler():void
	{
		
	}
	
	protected function clickToGoNextHandler():void
	{
		
	}
	
	protected function listItemSelectedHandler(render:IRenderProxy):void
	{
		
	}
}
}